/*
  ==============================================================================

    IfXmlHandler.cpp
    Created: 11 Jan 2015 12:58:55am
   

  ==============================================================================
*/

#include "IfXmlHandler.h"

IfXmlHandler::IfXmlHandler(File cFileHandle) {
	//m_xmlMainDocument.
	XmlDocument myDocument(cFileHandle);
	m_xmlMainElement = myDocument.getDocumentElement();
	if (m_xmlMainElement == nullptr)
	{
		printf("!!! Failed to load xml file: %s\n", myDocument.getLastParseError().toRawUTF8());
		exit(1);
	}
}

IfXmlHandler::~IfXmlHandler() {
	// TODO V Run through the list(s) and delete/destroy all the classes in the list.

}

int IfXmlHandler::LoadAllProjects() {
		printf("DDD XML structure version: %s\n", m_xmlMainElement->getStringAttribute("Version").toRawUTF8());

		// read the project:
		XmlElement *xmlProject = m_xmlMainElement->getChildByName("Project");
		while (xmlProject != nullptr) {
			printf("DDD TagName: %s\n", xmlProject->getTagName().toRawUTF8());
			// make getting the text part a sub function.
			XmlElement *xmlHeadline = xmlProject->getChildByName("Headline");
			printf("DDD Headline: %s\n", xmlHeadline->getAllSubText().toRawUTF8());
			DataProject *pDataProject = new DataProject;
			// TODO C Read all the relevant data from XML and put them into the Class.
			pDataProject->SetHeadline(xmlHeadline->getAllSubText());
			m_lstProjectData.push_back(pDataProject);
			
			xmlProject = xmlProject->getNextElementWithTagName("Project");
			// TODO V Add trace macros instead of printf statements, like the linux book described.
			printf("DDD xmlProject pointer: 0x%X\n", xmlProject);
		}

		//if (xmlProject != nullptr) delete xmlProject;
		return(0);
}


void IfXmlHandler::IterateAllProjects() {
	std::list<DataProject*>::iterator it;

	printf("III IfXmlHandler::IterateAllProjects()\n");

	for (it = m_lstProjectData.begin(); it != m_lstProjectData.end(); ++it) {
		DataProject *pMyProjectData = *it;
		printf("DDD Iterate: %s\n", pMyProjectData->GetHeadline().toRawUTF8());
	}
}